export default {
  data() {
    return {};
  },
  methods: {
    // 获取经纬度数据
    getLngLat(data) {
      if (Array.isArray(data)) {
        const [lng, lat] = data;
        return new window.T.LngLat(lng, lat);
      } else if (Object.prototype.toString.call(data) === '[object Object]') {
        const { lng, lat } = data;
        return new window.T.LngLat(lng, lat);
      }
      return null;
    },
    // 渲染mark
    addMarks({
      data = [],
      img = 'http://api.tianditu.gov.cn/img/map/markerA.png',
      size = [20, 20],
      anchor = [0, 0],
      zIndexOffset = 1,
      draggable = false,
      title = '',
      opacity = 1,
      onClick = null,
    }) {
      data.forEach(v => {
        const lnglat = this.getLngLat(v);
        const [w, h] = size;
        const [x, y] = anchor;
        if (lnglat) {
          const marker = new window.T.Marker(lnglat, {
            zIndexOffset,
            draggable,
            title,
            opacity,
            icon: new window.T.Icon({
              iconUrl: typeof img === 'function' ? img(v) : img,
              iconSize: new window.T.Point(w, h),
              iconAnchor: new window.T.Point(x, y),
            }),
          });
          if (onClick) {
            marker.addEventListener('click', e => onClick({ e, v }));
          }
          this.map.addOverLay(marker);
        }
      });
    },
    // 弹窗
  },
};
